main.controller('regCtrl', ['$scope','tools','SweetAlert','$location','$timeout', function ($scope, tools,SweetAlert,$location,$timeout) {


	if($(window).width()<=414){
		$scope.mobile = true;
	}else{
		$scope.mobile = false;
	}
	$('ul.tabs').tabs();
	$timeout(function(){
		$('ul.tabs').tabs('select_tab','phoneReg');
	},100);
	


	$scope.scond = 60;
	function setTime(){
        if($scope.scond!=0){
            $scope.scond--;
            $timeout(function(){                
                setTime();
            },1000);
        }else{
            $scope.User.getCodeStuff = false;
            $scope.scond = 60;
        }
    }
	// SweetAlert.swal('','注册失败','error');
	$scope.User = {
		oldName:'',
		name:'',
		password:'',
		password2:'',
		phone:'',
		email:'',
		nameError:'',
		passwordError:'',
		phoneError:'',
		emailError:'',
		getCodeStuff:false,
		getCode:function(){
			this.getCodeStuff = true;
			$timeout(function(){
				setTime();
			},1000);
		},
		checkName:function(){
			console.log(this.name);
			console.log(this.oldName);
			if(this.name == '' || this.name == this.oldName){
				return;
			}
			
			var $this = this;
			tools.$post({
				url:'/users/queryByName',
				data:{
					name:this.name
				}
			}).then(function(resp){
				$this.oldName = $this.name;
				console.log(resp);
				if(resp.value.length == 0){
					$this.nameError = "";
				}else{
					$this.nameError = "该用户名已存在";
				}
				
			},function(errResp){

				console.log('error')
			});
		},
		checkPassword:function(){
			if(this.password != this.password2){
				this.passwordError = '两次密码不一致';
				
			}else{
				this.passwordError = '';

			}
		},
		checkEmail:function(){
			var regExp = new RegExp(/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/);
			if(regExp.test(this.email)){
				this.emailError = '';
			}else{
				this.emailError = '邮箱格式错误';
			}
		},
		checkPhone:function(){
			var regExp = new RegExp(/^1[34578]\d{9}$/);
			if(regExp.test(this.phone)){
				this.phoneError = '';
			}else{
				this.phoneError = '手机格式错误';
			}
		},
		reg:function(){

			this.checkPassword();
			if(this.nameError !='' || this.passwordError != '' || this.emailError !='' || this.name=='' || this.password=='' || this.email ==''){
				SweetAlert.swal('','请填写正确的注册信息','warning');
				return;
			}
			tools.$post({
				url:'/users/insert',
				// url:'/proxy/marketconditions/user/userLogin.htm',
				data:{
					name: this.name,
					password: this.password,
					email: this.email
				}
			}).then(function(resp){
				console.log(resp);
				if(resp.value.insertId){
					SweetAlert.swal({
						title: "注册成功",
						type: "success",
						showCancelButton: false,
						confirmButtonColor: "#DD6B55",
						confirmButtonText: "去登录",
						closeOnConfirm: true
					},function(){ 
						$timeout(function(){
							$location.path('/login');
						},100);
					});
				}else{
					SweetAlert.swal('','注册失败','error');
				}
				
			},function(errResp){
				console.log('error');
			});
		}
	};


}]);